< previous page page_67 next page >

Page 67
choices. If you set up a program to handle only the words yes and no, your program will have a problem if users type Maybe or if they mistype a word.
You can eliminate this problem, however, and make your programs easier to use by employing controls to display and accept choices. In the following sections, you will examine several controls used for making choices. You can offer these choices through the use of check boxes, option buttons, lists, and combo boxes:
Check Box. Switches one or more options on or off
Option Button. Selects a single choice from a group
List Box. Displays a list of user-defined items
Combo Box. Like a list box, but also displays selected item
The Command Button
A control important to practically every application that you will develop is the CommandButton control. Typically, this control lets users initiate actions by clicking the button. You can set up a CommandButton control by drawing the button on the form and then setting its Caption property to the text that you want displayed on the button's face. To activate the button, just place code in the button's Click event procedure. Like any other event procedure, this code can consist of any number of valid Visual Basic programming statements.
Although users most often use command buttons by clicking them, some users prefer accessing commands through the keyboard versus using the mouse. This is often the case for data-entry intensive programs. To accommodate these users, you can make your program trigger command button events when certain keys are pressed. You do so by assigning an access key to the command button. When an access key is defined, the user holds down the Alt key and presses the access key to trigger the CommandButton control's Click event.
You assign an access key when you set the CommandButton control's Caption property. Simply place an ampersand (&) in front of the letter of the key you want to use. For example, if you want the users to be able to press Alt+P to run a print command button, you set the Caption property to &Print. The ampersand does not show up on the button, but the letter for the access key is underlined. The caption Print then appears on the command button.
note.gif If, for some reason, you need to display an ampersand in a command button caption, simply use two of them in a row in the Caption property; for example, Save && Exit produces the caption Save & Exit. square.gif

One command button on a form can be designated as the default button. Therefore, the user can simply press Enter while the focus is on any control (except another command button or a text box whose MultiLine property is True) to trigger the default button. This action triggers the default button's Click event, just as if the users had clicked it with the mouse. To set up a button as the default button, set its Default property to True. Only one button on a form can be the default button.

 
< previous page page_67 next page >